import logging
import smtplib
from email.mime.text import MIMEText
from email.header import Header

from price_analysis.core.config import settings
from price_analysis.utils import letter_template as lt

logger = logging.getLogger("main")


def send_mail(e_mail: str, message: str):

    server = smtplib.SMTP(settings.gm_smtp, settings.gm_port)
    server.starttls()

    try:
        server.login(settings.gm_sender, settings.google_pass)
        msg = MIMEText(message, "plain", "utf-8")
        msg["Subject"] = Header(lt.message_subject, "utf-8")
        server.sendmail(settings.gm_sender, e_mail, msg.as_string())
        return logger.info("Письмо успешно отправлено")

    except Exception as e:
        return logger.error(f"Ошибка отправки письма {e}")
